from odoo import fields, models, api, _
from odoo.exceptions import UserError


class SMrpBase(models.Model):
    _name = 's.mrp.base'
    _description = 'Mrp Base'
    _order = 'name'

    name = fields.Char('Base MO', required=True)
    product_id = fields.Many2one('product.product', 'Product')
    production_ids = fields.One2many('mrp.production', 's_base_mo_id', string='Production')

    # 生产订单数量
    product_qty = fields.Integer('production order qty', compute='compute_qty', store=True)

    # 成品报废数量
    finished_scrap_qty = fields.Integer('finished products scrap qty', compute='compute_scrap_qty', store=True)
    # 半成品报废数量
    semi_finished_scrap_qty = fields.Integer('semi-finished products scrap qty', compute='compute_scrap_qty', store=True)
    # 成品补投数量
    supplement_qty = fields.Integer('Supplement Qty', compute='compute_scrap_qty', store=True)

    scrap_ratio = fields.Float('Finished Scrap Ratio', compute='compute_scrap_qty', store=True)
    type = fields.Selection([
        ('sale', 'Sale'),
        ('rd', 'R&D')
    ], string='Type', compute='_compute_type', store=True)

    @api.depends('production_ids')
    def _compute_type(self):
        for rec in self:
            if rec.type:
                continue
            if not rec.production_ids:
                continue
            production = rec.production_ids[0]
            if production.s_sale_order_line_ids:
                rec.type = 'sale'
            else:
                rec.type = 'rd'

    @api.depends('production_ids', 'production_ids.qty_reject')
    def compute_qty(self):
        for rec in self:
            finished_production_ids = rec.production_ids.filtered(lambda x: x.product_id.categ_id.code and x.product_id.categ_id.code == 'CHP')
            semi_finished_production_ids = rec.production_ids.filtered(lambda x: x.product_id.categ_id.code and x.product_id.categ_id.code != 'CHP')
            product_qty = sum(finished_production_ids.mapped('product_qty'))
            finished_scrap_qty = sum(finished_production_ids.mapped('qty_reject'))
            semi_finished_scrap_qty = sum(semi_finished_production_ids.mapped('qty_reject'))
            supplement_qty = sum(finished_production_ids.filtered(lambda x: x.mo_type == 'M').mapped('product_qty'))

            rec.write({
                'product_qty': product_qty,
                'finished_scrap_qty': finished_scrap_qty,
                'semi_finished_scrap_qty': semi_finished_scrap_qty,
                'supplement_qty': supplement_qty,
                'scrap_ratio': finished_scrap_qty / product_qty if product_qty else 0,
            })

    def action_compute(self):
        production_ids = self.env['mrp.production'].search([])
        production_ids.constrains_base_mo()

    def action_feeding(self):
        if len(self) > 1:
            raise UserError(_('Not allowed to merge supplementary investment'))
        return {
            'name': _('Planned distribution of materials'),
            'type': 'ir.actions.act_window',
            'view_mode': 'form',
            'res_model': 's.supp.plan.material.requirement.wizard',
            'target': 'new',
            'context': {
                'base_id': self.id,
            },
        }
